package com.baiyu.total;

import android.content.Intent;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    List<CheckBox> checkBoxSum;//记录商品 清空用的  也就是购物车

    RadioButton weiXinPay;
    CheckBox checkBox1;//汉堡
    CheckBox checkBox2;//可乐

    CheckBox checkBox3;//鸡翅

    CheckBox checkBox4;//薯条



    Button payButton;
    Button clearButton;//清空


    ImageView meishiyiimageViewsub;
    ImageView meishieimageViewsub;
    ImageView meishisanimageViewsub;
    ImageView meishisimageViewsub;


    ImageView meishiyiimageViewadd;
    ImageView meishieimageViewadd;
    ImageView meishisanimageViewadd;
    ImageView meishisiimageViewadd;


    TextView meishiyitextView;
    TextView meishietextView;
    TextView meishisantextView;
    TextView meishisitextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        checkBoxSum=new ArrayList<>();

        checkBox1=findViewById(R.id.apple);
        checkBox2=findViewById(R.id.watermalen);
        checkBox3=findViewById(R.id.jichi);
        checkBox4=findViewById(R.id.shutiao);
        weiXinPay=findViewById(R.id.weixinpay);

        payButton=findViewById(R.id.pay);
        clearButton=findViewById(R.id.clear);


        meishiyiimageViewsub=findViewById(R.id.meishiyisub);
        meishieimageViewsub=findViewById(R.id.meishiesub);
        meishisanimageViewsub=findViewById(R.id.meishisansub);
        meishisimageViewsub=findViewById(R.id.meishisisub);

        meishiyiimageViewsub.setOnClickListener(this);
        meishieimageViewsub.setOnClickListener(this);
        meishisanimageViewsub.setOnClickListener(this);
        meishisimageViewsub.setOnClickListener(this);



        meishiyiimageViewadd=findViewById(R.id.meishiyiadd);
        meishieimageViewadd=findViewById(R.id.meishieadd);
        meishisanimageViewadd = findViewById(R.id.meishisanadd);
        meishisiimageViewadd=findViewById(R.id.meishisiadd);

        meishiyiimageViewadd.setOnClickListener(this);
        meishieimageViewadd.setOnClickListener(this);
        meishisanimageViewadd.setOnClickListener(this);
        meishisiimageViewadd.setOnClickListener(this);





        meishiyitextView=findViewById(R.id.meishiyifene);
        meishietextView=findViewById(R.id.meishiefene);
        meishisantextView=findViewById(R.id.meishisanfene);
        meishisitextView=findViewById(R.id.meishisifene);



        payButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                jieZhang();
            }
        });



        clearButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                for (CheckBox cb: checkBoxSum) {

                    cb.setChecked(false);
                }
            }
        });





    }



    private void jieZhang() {
        int sum=0;//价格
        //多少份
        int yipart= Integer.valueOf(meishiyitextView.getText().toString());
        int epart= Integer.valueOf(meishietextView.getText().toString());
        int sanpart= Integer.valueOf(meishisantextView.getText().toString());
        int sipart= Integer.valueOf(meishisitextView.getText().toString());

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this)
                .setTitle("账单:")
                ;

        String str="";
        if(checkBox1.isChecked()){

            str =str+ "商品:美食1 价格:1元\n";
            sum=sum+1*yipart;
            checkBoxSum.add(checkBox1);
        }

        if(checkBox2.isChecked()){

            str=str + "商品:美食2 价格:2元\n";

            sum=sum+2*epart;
            checkBoxSum.add(checkBox2);
        }

        if(checkBox3.isChecked()){
            str=str + "商品:美食3 价格:3元\n";

            sum=sum+3*sanpart;
            checkBoxSum.add(checkBox3);

        }

        if (checkBox4.isChecked()) {
            str=str + "商品:美食4 价格:4元\n";

            sum=sum+4*sipart;

            checkBoxSum.add(checkBox4);
        }



        String str99="总价格:"+sum+"\n";

        str=str+str99;

       // alertDialog.setMessage(str);
       // alertDialog.create();
       // alertDialog.show();


        Intent intent=new Intent(MainActivity.this,OtherActivity.class);
        intent.putExtra("jieguo",str);
        intent.putExtra("pay",weiXinPay.isChecked());
        startActivity(intent);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.meishiyisub:
                Operation.meishiyisub(meishiyitextView);
                break;
            case R.id.meishiesub:
                Operation.meishiesub(meishietextView);
                break;
            case R.id.meishisansub:
                Operation.meishisansub(meishisantextView);
                break;
            case R.id.meishisisub:
                Operation.meishisisub(meishisitextView);
                break;
            case R.id.meishiyiadd:
                Operation.meishiyiadd(meishiyitextView);
                break;
            case R.id.meishieadd:
                Operation.meishieadd(meishietextView);
                break;
            case R.id.meishisanadd:
                Operation.meishisanadd(meishisantextView);
                break;
            case R.id.meishisiadd:
                Operation.meishisiadd(meishisitextView);
                break;

        }
    }
}